﻿/******************************************************************************
文件名称    :   MySqlPatientDataProvider.cs
创建日期    :   2011/5/11 22:00:48
功能描述    :   病人信实体数据提供程序MySql实现类
修改日志    :   
    2011-05-11 22:00  马利
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Dare.Utilities.Data;
using Stone.Data.Providers;
using Stone.Components.Entities;
using MySql.Data.MySqlClient;

namespace Stone.Data.MySql.Providers
{
    /// <summary>
    /// 病人信实体数据提供程序MySql实现类
    /// </summary>
    public class MySqlPatientDataProvider : PatientDataProvider
    {
        public MySqlPatientDataProvider()
            : base()
        {
        }

        public override void Insert(TransactionManager manager, Patient entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_patient(name, name_abbr, sex, birthday, age, height, weight, number, BMI, address, telephone, cellphone, postcode, history_id, has_menses, menses_cycle, Amenorrhea_time, comment) values(?name, ?name_abbr, ?sex, ?birthday, ?age, ?height, ?weight, ?number, ?BMI, ?address, ?telephone, ?cellphone, ?postcode, ?history_id, ?has_menses, ?menses_cycle, ?Amenorrhea_time, ?comment);" +
                    "select id, time_stamp from tb_patient where id = last_insert_id();";
                GenericDbHelper.AddParameter(mgr, cmd, "?name", DbType.String, 18).Value = entity.Name == null ? String.Empty : entity.Name;
                GenericDbHelper.AddParameter(mgr, cmd, "?name_abbr", DbType.String, 10).Value = entity.NameAbbr == null ? DBNull.Value : (object)entity.NameAbbr;
                GenericDbHelper.AddParameter(mgr, cmd, "?sex", DbType.UInt16).Value = entity.Sex;
                GenericDbHelper.AddParameter(mgr, cmd, "?birthday", DbType.Date).Value = entity.Birthday == null ? DBNull.Value : (object)entity.Birthday.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?age", DbType.Int32).Value = entity.Age == null ? DBNull.Value : (object)entity.Age.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?height", DbType.Decimal).Value = entity.Height == null ? DBNull.Value : (object)entity.Height.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?weight", DbType.Decimal).Value = entity.Weight == null ? DBNull.Value : (object)entity.Weight.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?number", DbType.Int32).Value = entity.Number == null ? DBNull.Value : (object)entity.Number.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?BMI", DbType.Decimal).Value = entity.Bmi == null ? DBNull.Value : (object)entity.Bmi.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?address", DbType.String, 200).Value = entity.Address == null ? DBNull.Value : (object)entity.Address;
                GenericDbHelper.AddParameter(mgr, cmd, "?telephone", DbType.String, 20).Value = entity.Telephone == null ? DBNull.Value : (object)entity.Telephone;
                GenericDbHelper.AddParameter(mgr, cmd, "?cellphone", DbType.String, 20).Value = entity.Cellphone == null ? DBNull.Value : (object)entity.Cellphone;
                GenericDbHelper.AddParameter(mgr, cmd, "?postcode", DbType.String, 6).Value = entity.Postcode == null ? DBNull.Value : (object)entity.Postcode;
                GenericDbHelper.AddParameter(mgr, cmd, "?history_id", DbType.Int64).Value = entity.HistoryId == null ? DBNull.Value : (object)entity.HistoryId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?has_menses", DbType.UInt16).Value = entity.HasMenses == null ? DBNull.Value : (object)entity.HasMenses.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?menses_cycle", DbType.Int32).Value = entity.MensesCycle == null ? DBNull.Value : (object)entity.MensesCycle.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?Amenorrhea_time", DbType.Int32).Value = entity.AmenorrheaTime == null ? DBNull.Value : (object)entity.AmenorrheaTime.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;

                //执行数据库操作,并获得主键和更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.Id = reader.GetInt32(0);
                        entity.TimeStamp = reader.GetDateTime(1);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //插入关联
                InsertRelations(mgr, cmd, entity);

                //UpdatePatientHospitalizations(manager, entity.Id, entity.Hospitalization);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        //private void UpdatePatientHospitalizations(TransactionManager manager, long id, List<Hospitalization> list)
        //{
        //    Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
        //    {
        //        /*------------------------------数据库SQL操作开始----------------------------------*/

        //        cmd.CommandText = "delete from tb_hospitalization where patient_id=?patient_id;";
        //        GenericDbHelper.AddParameter(manager, cmd, "?patient_id", DbType.Int64).Value = id;
        //        cmd.ExecuteNonQuery();
        //        
        //        //if (list != null)
        //        //{
        //        //    cmd.CommandText = "insert into tb_hospitalization(terminal_id, group_id, start_time, end_time, period_type, period_value, is_exclude, pri, setting_type, setting_value,week) values(?terminal_id, ?group_id, ?start_time, ?end_time, ?period_type, ?period_value, ?is_exclude, pri, ?setting_type, ?setting_value, ?week)";
        //        //    cmd.Parameters.Clear();
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?terminal_id", DbType.Int32).Value = id;
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?group_id", DbType.Int32);
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?start_time", DbType.Int32);
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?end_time", DbType.Int32);
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?period_type", DbType.Int32);
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?period_value", DbType.Int32);
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?is_exclude", DbType.Int16);
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?pri", DbType.Int32);
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?setting_type", DbType.Int32);
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?setting_value", DbType.Int32);
        //        //    GenericDbHelper.AddParameter(mgr, cmd, "?week", DbType.Int32);

        //        //    foreach (Hospitalization hos in list)
        //        //    {
        //        //        int i = 1;
        //        //        cmd.Parameters[i++].Value = hos.GroupID;
        //        //        cmd.Parameters[i++].Value = hos.StartTime;
        //        //        cmd.Parameters[i++].Value = hos.EndTime;
        //        //        cmd.Parameters[i++].Value = hos.PeriodType;
        //        //        cmd.Parameters[i++].Value = hos.PeriodValue;
        //        //        cmd.Parameters[i++].Value = hos.IsExclude;
        //        //        cmd.Parameters[i++].Value = hos.PRI;
        //        //        cmd.Parameters[i++].Value = hos.SettingType;
        //        //        cmd.Parameters[i++].Value = hos.SettingValue;
        //        //        cmd.Parameters[i++].Value = hos.Week;

        //        //        cmd.ExecuteNonQuery();
        //        //    }
        //        //}

        //        /*------------------------------数据库SQL操作结束----------------------------------*/
        //    });
        //}

        public override void Import(TransactionManager manager, IEnumerable<Patient> entities)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_patient(id, name, name_abbr, sex, birthday, age, height, weight, number, BMI, address, telephone, cellphone, postcode, history_id, has_menses, menses_cycle, Amenorrhea_time, comment, time_stamp) values(?id, ?name, ?name_abbr, ?sex, ?birthday, ?age, ?height, ?weight, ?number, ?BMI, ?address, ?telephone, ?cellphone, ?postcode, ?history_id, ?has_menses, ?menses_cycle, ?Amenorrhea_time, ?comment, ?time_stamp);";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?name", DbType.String, 18);
                GenericDbHelper.AddParameter(mgr, cmd, "?name_abbr", DbType.String, 10);
                GenericDbHelper.AddParameter(mgr, cmd, "?sex", DbType.UInt16);
                GenericDbHelper.AddParameter(mgr, cmd, "?birthday", DbType.Date);
                GenericDbHelper.AddParameter(mgr, cmd, "?age", DbType.Int32);
                GenericDbHelper.AddParameter(mgr, cmd, "?height", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?weight", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?number", DbType.Int32);
                GenericDbHelper.AddParameter(mgr, cmd, "?BMI", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?address", DbType.String, 200);
                GenericDbHelper.AddParameter(mgr, cmd, "?telephone", DbType.String, 20);
                GenericDbHelper.AddParameter(mgr, cmd, "?cellphone", DbType.String, 20);
                GenericDbHelper.AddParameter(mgr, cmd, "?postcode", DbType.String, 6);
                GenericDbHelper.AddParameter(mgr, cmd, "?history_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?has_menses", DbType.UInt16);
                GenericDbHelper.AddParameter(mgr, cmd, "?menses_cycle", DbType.Int32);
                GenericDbHelper.AddParameter(mgr, cmd, "?Amenorrhea_time", DbType.Int32);
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?time_stamp", DbType.DateTime);

                //执行数据库操作
                foreach (Patient entity in entities)
                {
                    int i = 0;
                    cmd.Parameters[i++].Value = entity.Id;
                    cmd.Parameters[i++].Value = entity.Name == null ? String.Empty : entity.Name;
                    cmd.Parameters[i++].Value = entity.NameAbbr == null ? DBNull.Value : (object)entity.NameAbbr;
                    cmd.Parameters[i++].Value = entity.Sex;
                    cmd.Parameters[i++].Value = entity.Birthday == null ? DBNull.Value : (object)entity.Birthday.Value;
                    cmd.Parameters[i++].Value = entity.Age == null ? DBNull.Value : (object)entity.Age.Value;
                    cmd.Parameters[i++].Value = entity.Height == null ? DBNull.Value : (object)entity.Height.Value;
                    cmd.Parameters[i++].Value = entity.Weight == null ? DBNull.Value : (object)entity.Weight.Value;
                    cmd.Parameters[i++].Value = entity.Number == null ? DBNull.Value : (object)entity.Number.Value;
                    cmd.Parameters[i++].Value = entity.Bmi == null ? DBNull.Value : (object)entity.Bmi.Value;
                    cmd.Parameters[i++].Value = entity.Address == null ? DBNull.Value : (object)entity.Address;
                    cmd.Parameters[i++].Value = entity.Telephone == null ? DBNull.Value : (object)entity.Telephone;
                    cmd.Parameters[i++].Value = entity.Cellphone == null ? DBNull.Value : (object)entity.Cellphone;
                    cmd.Parameters[i++].Value = entity.Postcode == null ? DBNull.Value : (object)entity.Postcode;
                    cmd.Parameters[i++].Value = entity.HistoryId == null ? DBNull.Value : (object)entity.HistoryId.Value;
                    cmd.Parameters[i++].Value = entity.HasMenses == null ? DBNull.Value : (object)entity.HasMenses.Value;
                    cmd.Parameters[i++].Value = entity.MensesCycle == null ? DBNull.Value : (object)entity.MensesCycle.Value;
                    cmd.Parameters[i++].Value = entity.AmenorrheaTime == null ? DBNull.Value : (object)entity.AmenorrheaTime.Value;
                    cmd.Parameters[i++].Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                    cmd.Parameters[i++].Value = entity.TimeStamp;
                    cmd.ExecuteNonQuery();
                }

                //插入关联
                InsertRelations(mgr, cmd, entities);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Update(TransactionManager manager, Patient entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //更新SQL语句
                cmd.CommandText = "update tb_patient set name = ?name, name_abbr = ?name_abbr, sex = ?sex, birthday = ?birthday, age = ?age, height = ?height, weight = ?weight, number = ?number, BMI = ?BMI, address = ?address, telephone = ?telephone, cellphone = ?cellphone, postcode = ?postcode, history_id = ?history_id, has_menses = ?has_menses, menses_cycle = ?menses_cycle, Amenorrhea_time = ?Amenorrhea_time, comment = ?comment where id = ?id;"
                        + "select time_stamp from tb_patient where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?name", DbType.String, 18).Value = entity.Name == null ? String.Empty : entity.Name;
                GenericDbHelper.AddParameter(mgr, cmd, "?name_abbr", DbType.String, 10).Value = entity.NameAbbr == null ? DBNull.Value : (object)entity.NameAbbr;
                GenericDbHelper.AddParameter(mgr, cmd, "?sex", DbType.UInt16).Value = entity.Sex;
                GenericDbHelper.AddParameter(mgr, cmd, "?birthday", DbType.Date).Value = entity.Birthday == null ? DBNull.Value : (object)entity.Birthday.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?age", DbType.Int32).Value = entity.Age == null ? DBNull.Value : (object)entity.Age.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?height", DbType.Decimal).Value = entity.Height == null ? DBNull.Value : (object)entity.Height.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?weight", DbType.Decimal).Value = entity.Weight == null ? DBNull.Value : (object)entity.Weight.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?number", DbType.Int32).Value = entity.Number == null ? DBNull.Value : (object)entity.Number.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?BMI", DbType.Decimal).Value = entity.Bmi == null ? DBNull.Value : (object)entity.Bmi.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?address", DbType.String, 200).Value = entity.Address == null ? DBNull.Value : (object)entity.Address;
                GenericDbHelper.AddParameter(mgr, cmd, "?telephone", DbType.String, 20).Value = entity.Telephone == null ? DBNull.Value : (object)entity.Telephone;
                GenericDbHelper.AddParameter(mgr, cmd, "?cellphone", DbType.String, 20).Value = entity.Cellphone == null ? DBNull.Value : (object)entity.Cellphone;
                GenericDbHelper.AddParameter(mgr, cmd, "?postcode", DbType.String, 6).Value = entity.Postcode == null ? DBNull.Value : (object)entity.Postcode;
                GenericDbHelper.AddParameter(mgr, cmd, "?history_id", DbType.Int64).Value = entity.HistoryId == null ? DBNull.Value : (object)entity.HistoryId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?has_menses", DbType.UInt16).Value = entity.HasMenses == null ? DBNull.Value : (object)entity.HasMenses.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?menses_cycle", DbType.Int32).Value = entity.MensesCycle == null ? DBNull.Value : (object)entity.MensesCycle.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?Amenorrhea_time", DbType.Int32).Value = entity.AmenorrheaTime == null ? DBNull.Value : (object)entity.AmenorrheaTime.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = entity.Id;

                //执行数据库操作,并获得更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.TimeStamp = reader.GetDateTime(0);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //更新关联
                UpdateRelations(mgr, cmd, entity);

                //UpdatePatientHospitalizations(manager, entity.Id, entity.Hospitalization);


                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Delete(TransactionManager manager, long key)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, key);

                //删除SQL语句
                cmd.CommandText = "delete from tb_patient where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void DeleteAll(TransactionManager manager, string whereClause)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------具体SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, whereClause);

                //删除SQL语句
                cmd.CommandText = String.Format("delete from tb_patient {0}",
                     ProcessWhereClause(whereClause));

                //执行
                cmd.ExecuteNonQuery();

                /*------------------------------具体SQL操作结束----------------------------------*/
            });
        }

        public override Patient Get(TransactionManager manager, long key)
        {
            Patient entity = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = "select * from tb_patient where id = ?id";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行获取实体
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity = Fill(reader, null);
                    }
                }

                //获取关联
                if (entity != null)
                {
                    RetrieveRelations(mgr, cmd, entity);
                    entity.EntityState = Stone.Components.EntityState.Saved;
                }

                //if (entity != null)
                //{
                //    entity.Hospitalization = new List<Hospitalization>();

                //    cmd.CommandText = "select date, chemistry_id, optics_id, surgery_id from tb_hospitalization where patient_id=?patient_id";
                //    cmd.Parameters.Clear();
                //    GenericDbHelper.AddParameter(mgr, cmd, "?patient_id", DbType.Int64).Value = entity.Id;

                //    using (IDataReader reader = GenericDbHelper.ExecuteReader(mgr, cmd))
                //    {
                //        while (reader.Read())
                //        {
                //            Hospitalization hos = new Hospitalization();
                //            hos.Date= reader.GetDateTime(0);
                //            hos.ChemistryId= reader.GetInt64(1);
                //            hos.OpticsId= reader.GetInt64(2);
                //            hos.SurgeryId = reader.GetInt64(3);

                //            entity.Hospitalization.Add(hos);
                //        }
                //    }
                //}


                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return entity;
        }

        public override List<Patient> GetPaged(TransactionManager manager, string whereClause, string orderBy, int pageIndex, int pageSize)
        {
            List<Patient> list = new List<Patient>(pageSize > 0 ? Math.Min(pageSize, 256) : 32);
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select * from tb_patient {0} {1} {2}",
                    ProcessWhereClause(whereClause),
                    ProcessOrderBy(orderBy),
                    ProcessPaging(pageIndex, pageSize));

                //执行获取实体集
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Fill(reader, null));
                    }
                }

                //获取关联
                RetrieveRelations(mgr, cmd, list);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return list;
        }

        public override int GetCount(TransactionManager manager, string whereClause)
        {
            int count = 0;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*) from tb_patient {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                count = Convert.ToInt32(cmd.ExecuteScalar());

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return count;
        }

        public override long GetVersion(TransactionManager manager, string whereClause)
        {
            long count = 0;
            DateTime? timeStamp = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*), max(time_stamp) from tb_patient {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt64(0);
                        timeStamp = reader.IsDBNull(1) ? (DateTime?)null : reader.GetDateTime(1);
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return (timeStamp ?? DateTime.MinValue).Ticks ^ count;
        }

        public override Patient Fill(IDataReader reader, Patient entity)
        {
            if (entity == null) entity = new Patient();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                switch (reader.GetName(i))
                {
                    case "id":
                        entity.Id = reader.GetInt64(i);
                        break;
                    case "name":
                        entity.Name = reader.GetString(i);
                        break;
                    case "name_abbr":
                        entity.NameAbbr = reader[i] == DBNull.Value ? entity.NameAbbr : reader.GetString(i);
                        break;
                    case "sex":
                        entity.Sex = DbConvert.ToNullableEnum<SexEnum>(reader[i]) ?? SexEnum.male;
                        break;
                    case "birthday":
                        entity.Birthday = reader[i] == DBNull.Value ? entity.Birthday : reader.GetDateTime(i);
                        break;
                    case "age":
                        entity.Age = reader[i] == DBNull.Value ? entity.Age : reader.GetInt32(i);
                        break;
                    case "height":
                        entity.Height = reader[i] == DBNull.Value ? entity.Height : reader.GetDecimal(i);
                        break;
                    case "weight":
                        entity.Weight = reader[i] == DBNull.Value ? entity.Weight : reader.GetDecimal(i);
                        break;
                    case "number":
                        entity.Number = reader[i] == DBNull.Value ? entity.Number : reader.GetInt32(i);
                        break;
                    case "BMI":
                        entity.Bmi = reader[i] == DBNull.Value ? entity.Bmi : reader.GetDecimal(i);
                        break;
                    case "address":
                        entity.Address = reader[i] == DBNull.Value ? entity.Address : reader.GetString(i);
                        break;
                    case "telephone":
                        entity.Telephone = reader[i] == DBNull.Value ? entity.Telephone : reader.GetString(i);
                        break;
                    case "cellphone":
                        entity.Cellphone = reader[i] == DBNull.Value ? entity.Cellphone : reader.GetString(i);
                        break;
                    case "postcode":
                        entity.Postcode = reader[i] == DBNull.Value ? entity.Postcode : reader.GetString(i);
                        break;
                    case "history_id":
                        entity.HistoryId = reader[i] == DBNull.Value ? entity.HistoryId : reader.GetInt64(i);
                        break;
                    case "has_menses":
                        entity.HasMenses = reader[i] == DBNull.Value ? entity.HasMenses : reader.GetBoolean(i);
                        break;
                    case "menses_cycle":
                        entity.MensesCycle = reader[i] == DBNull.Value ? entity.MensesCycle : reader.GetInt32(i);
                        break;
                    case "Amenorrhea_time":
                        entity.AmenorrheaTime = reader[i] == DBNull.Value ? entity.AmenorrheaTime : reader.GetInt32(i);
                        break;
                    case "comment":
                        entity.Comment = reader[i] == DBNull.Value ? entity.Comment : reader.GetString(i);
                        break;
                    case "time_stamp":
                        entity.TimeStamp = reader.GetDateTime(i);
                        break;
                }
            }

            entity.EntityState = Stone.Components.EntityState.Saved;
            return entity;
        }

        #region 实体关联操作数据提供类

        #endregion

        public override List<Patient> SearchPatientInfo(TransactionManager manager, string querySql, string patientName, string orderBy, int pageIndex, int pageSize, out int totalCount)
        {
            List<DbParameter> dbParams = new List<DbParameter>();
            StringBuilder sql = new StringBuilder();
            StringBuilder sqlCount = new StringBuilder();

            //sql.Append("select ti.mac, ti.name, ti.city, ti.location, ti.expired_date, ti.model, (select name from tb_terminal_group_info where id=ti.group_id) group_name, tc.heartbeat_interval, tc.output_type, tc.output_mode, ts.* from tb_terminal_info ti inner join tb_terminal_status ts on ts.terminal_id = ti.id left join tb_terminal_config tc on ti.id = tc.terminal_id where corp_id=?corp_id");
            //sqlCount.Append("select count(*) from tb_terminal_info ti inner join tb_terminal_status ts on ts.terminal_id = ti.id where corp_id=?corp_id");
            sql.Append("select * from tb_patient ");
            sqlCount.Append("select count(*) from tb_patient ");

            if (!String.IsNullOrEmpty(querySql))
            {
                sql.Append(querySql);
                sqlCount.Append(querySql);
            }
            else
            {
                if (!String.IsNullOrEmpty(patientName))
                {
                    sql.Append(" where name like ?name");
                    sqlCount.Append(" where name like ?name");
                    dbParams.Add(MySqlClientUtils.CreateParameter("?name", MySqlDbType.String, "%" + patientName + "%"));
                }
            }

            if (!String.IsNullOrEmpty(orderBy))
            {
                sql.Append(" ").Append(ProcessOrderBy(orderBy));
            }

            sql.Append(" ").Append(ProcessPaging(pageIndex, pageSize));

            return InternalGetPaged<Patient>(manager, sql.ToString(), sqlCount.ToString(), Fill, null, pageIndex, pageSize, out totalCount, dbParams.ToArray());
        }

        //private Patient FillPatient(IDataReader reader, Patient entity)
        //{
        //    if (entity == null) entity = new Patient();
        //    for (int i = 0; i < reader.FieldCount; i++)
        //    {
        //        switch (i)
        //        {
        //            //case 0:
        //            //    entity.Mac = new MacAddressData(reader.GetInt64(i));
        //            //    break;
        //            case 1:
        //                entity.Name = reader[i] == DBNull.Value ? String.Empty : reader.GetString(i);
        //                break;
        //            //case 2:
        //            //    entity.Sex = reader[i] == DBNull.Value ? String.Empty : reader.GetString(i);
        //            //    break;
        //            case 3:
        //                entity.Birthday = reader[i] == DBNull.Value ? null : (DateTime?)reader.GetDateTime(i);
        //                break;
        //            case 4:
        //                entity.Age = reader[i] == DBNull.Value ? String.Empty : reader.GetInt32(i);
        //                break;
        //            case 5:
        //                entity.Model = reader[i] == DBNull.Value ? String.Empty : reader.GetString(i);
        //                break;
        //            case 6:
        //                entity.GroupName = reader[i] == DBNull.Value ? String.Empty : reader.GetString(i);
        //                break;
        //            case 7:
        //                entity.HeartbeatInterval = TimeSpan.FromSeconds(reader.GetInt32(i));
        //                break;
        //            //case 8:
        //            //    entity.OutputType = (TerminalCommand.TerminalVideoOutputType)reader.GetInt32(i);
        //            //    break;
        //            //case 9:
        //            //    entity.OutputMode = EnumUtil.GetEnumFromDbEnumAttributeName<TerminalCommand.TerminalVideoOutputMode>(Convert.ToString(reader[i])) ?? entity.OutputMode;
        //            //    break;
        //            default:
        //                Fill(entity, reader.GetName(i), reader[i]);
        //                break;
        //        }
        //    }
        //    return entity;
        //}


    }
}
